package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.UserLoginConverter;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginMsg;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xD
 * @since 2023/01/10 10:14
 */
@Service
public class ILoginServiceImpl implements ILoginService {

    @Autowired
    UrlUtils urlUtils;

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private UserLoginConverter userLoginConverter;

    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        //通过用户名获取member
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", request.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() != 1) {
            throw new ValidateException(SysRetCodeConstants.USERNAME_ERROR.getCode(), SysRetCodeConstants.USERNAME_ERROR.getMessage());
        }
        // 1) 验证密码
        Member member = members.get(0);
        String password = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());
        if (!member.getPassword().equals(password)) {
            throw new ValidateException(SysRetCodeConstants.PASSWORD_ERROR.getCode(), SysRetCodeConstants.PASSWORD_ERROR.getMessage());
        }
        // 2) 验证邮箱 N 未验证  Y 已验证
        if ("N".equals(member.getIsVerified())) {
            throw new ValidateException(UserRetCode.USER_ISVERFIED_ERROR.getCode(), UserRetCode.USER_ISVERFIED_ERROR.getMessage());
        }
        // 3) 设置token
        UserLoginMsg userLoginMsg = new UserLoginMsg();
        userLoginMsg.setUid(member.getId());
        userLoginMsg.setUsername(member.getUsername());
        String msg = JSON.toJSONString(userLoginMsg);
        String token = JwtTokenUtils.builder().msg(msg).build().creatJwtToken();

        if (member.getFile() != null) {
            String url = urlUtils.getUrl(member.getFile());
            member.setFile(url);
        }

        return userLoginConverter.userLoginConverter(member, token);
    }


    //验证token
    @Override
    public UserLoginMsg verifyToken(String token) {
        //解密token 通过JwtTokenUtils
        String msg = JwtTokenUtils.builder().token(token).build().freeJwt();
        UserLoginMsg userLoginMsg = JSON.parseObject(msg, UserLoginMsg.class);
        Long uid = userLoginMsg.getUid();
        String username = userLoginMsg.getUsername();
        //数据库校验token
        Member member = memberMapper.selectByPrimaryKey(uid);
        if (!member.getUsername().equals(username)) {
            throw new ValidateException(SysRetCodeConstants.GET_TOKEN_FAILED.getCode(), SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
        }
        return userLoginMsg;
    }

    @Override
    public String exprieToken(String accessToken) {
        return JwtTokenUtils.builder().msg(accessToken).build().exprieJwtToken();
    }
}
